import { IEditorInfo } from "./views/EditorView/IEditorInfo";
import type { Slot } from "./views/WorkspacesView"

export interface IUnknowKindProps<D extends {} = {}> extends
  React.HTMLAttributes<HTMLDivElement>,
  IViewComponentProps<D> {
}
export function UnknowKind(props: IUnknowKindProps) {
  const { ws_slot, ..._p } = props;
  return <div {..._p}>{ws_slot?.id}</div>
}
export interface IViewComponentProps<D extends {} = {}> {
  ws_slot?: Slot,
  actived?: boolean,
  info?: IEditorInfo<D>
}
export type ViewComponent = React.ComponentType<IViewComponentProps>
export type ViewComponentWithKind = ViewComponent & { kind: string }

let _uid = 0;
class ViewsFactory {
  get new_uid() {
    return '' + (++_uid)
  }
  components: Record<string, ViewComponent> = {}
  register(component: ViewComponentWithKind): void
  register(kind: string, component: ViewComponent): void
  register(p1: string | ViewComponentWithKind, p2?: ViewComponent) {
    if (typeof p1 === 'string' && p2) this.components[p1] = p2;
    else if (typeof p1 !== 'string') this.components[p1.kind] = p1
  }

  find_component(kind: string | undefined, fallback: ViewComponent = UnknowKind): ViewComponent {
    if (!kind) return fallback
    return this.components[kind] ?? fallback;
  }
}
export const views_factory = new ViewsFactory()